const { ccclass, property } = cc._decorator;

@ccclass
export default class WayBaseControl extends cc.Component {
    @property
    offsetUp: number = 0;//路面宽度
    @property
    floorY: number = 0;//地面高度
    @property
    offsetDown: number = 0;
    @property
    friction: number = 0.2;//碰撞体摩擦力系数[0~1]
    @property
    restitution: number = 0.8;//碰撞体的弹性系数[0~1]
    @property
    speed: number = 0;
    @property
    nodeLength: number = 0;
    @property
    indexWidth: number = 0;
    @property
    wayId: string = null;
    occupyList: boolean[] = [];
    PutBodyIds: string[] = [];
    PutBoxIds: string[] = [];

    //记录被放置技能占用的位置
    setOccupy(index: number, bl: boolean) {
        this.occupyList[index] = bl;
    }
    //获取放置技能占用的位置
    getOccupy(): boolean[] {
        this.updateOccupyIndex();
        return this.occupyList;
    }
    //记录放置技能的位置
    setPutOccupy(index: number, _uuid: string) {
        if (this.PutBodyIds.length == 0) {
            for (let index = 0; index < this.occupyList.length; index++) {
                this.PutBodyIds.push(null);
            }
        }
        this.occupyList[index] = true;
        this.PutBodyIds[index] = _uuid;
    }
    //记录Box的初始位置
    addBoxOccupy(_uuid: string) {
        this.PutBoxIds.push(_uuid);
    }
    //记录Box的初始位置
    removeBoxOccupy(_uuid: string) {
        this.PutBoxIds.push(_uuid);
    }
    //更新放置技能位置
    updateOccupyIndex() {
        let i = 0;
        let self = this;
        this.PutBodyIds.forEach(element => {
            if (element) {
                let node = self.node.getChildByUuid(element);
                if (!node) {
                    self.occupyList[i] = false;
                    self.PutBodyIds[i] = null;
                }
            }
            i++;
        });
    }
    //获取随机空位置
    getEmptyIndex(w: number): number {
        let index = 0;
        let check = false;
        let checki = 0;
        let checky = 0;
        do {
            index = Math.floor(Math.random() * this.nodeLength);
            check = this.occupyList[index];
            if (!check) {
                let ax = this.getItemNodeX(index);
                let p1 = this.node.convertToWorldSpaceAR(cc.v2(ax, this.floorY));
                var a = new cc.Rect(p1.x, p1.y, 50, 50);
                check = this.checkBoxIndex(index, a);
                let _w = w / 2;
                if (!check) {
                    check = this.checkBodyIndex(index, cc.v2(p1.x - _w, p1.y + 50), cc.v2(p1.x + _w, p1.y + 50));
                    checki++;
                    if (checki >= 3)
                        break;
                }
            }
            checky++;
            if (checky >= 10)
                break;
        } while (check);
        if (check && (checki == 3 || checki == 3))
            return -1;
        return index;
    }
    //获取指定位置的横坐标
    getItemNodeX(_index: number): number {
        if (_index == 0 && this.nodeLength == 1)
            return 0;
        return this.node.children.filter(item => item.name == "way")[_index].x;
    }
    //校验指定位置是否被Box占用
    checkBoxIndex(_index: number, a: cc.Rect): boolean {
        if (this.PutBoxIds?.length > 0) {
            for (let index = 0; index < this.PutBoxIds.length; index++) {
                const element = this.PutBoxIds[index];
                var box = this.node.parent.getChildByUuid(element);
                let p2 = box.convertToWorldSpaceAR(cc.Vec2.ZERO);
                let b = new cc.Rect(p2.x, p2.y, box.width, box.height);
                if (a.intersects(b))
                    return true;
            }
        }
        return false;
    }
    //校验指定位置是否有怪兽
    checkBodyIndex(_index: number, p1: cc.Vec2, p2: cc.Vec2): boolean {
        var results = cc.director.getPhysicsManager().rayCast(p1, p2, cc.RayCastType.Any);
        if (results?.length > 0)
            return true;
        return false;
    }
    //初始化碰撞框
    setCollider(isBoxCollider, TagWayBody) {
        let nodeSize = this.node.getContentSize();
        nodeSize.height = nodeSize.height - this.offsetUp - this.offsetDown;
        let size = this.node.getContentSize();
        this.floorY = size.height / 2 - this.offsetUp;
        //设置路基刚体
        let collider = this.node.getComponent(cc.PhysicsBoxCollider);
        collider.friction = this.friction;
        collider.restitution = this.restitution;
        collider.size = nodeSize;
        collider.offset.y = (this.offsetDown - this.offsetUp) / 2;
        collider.tag = TagWayBody;
        //设置路面碰撞体
        let area = this.node.addComponent(cc.BoxCollider);
        area.tag = TagWayTrack;
        area.offset.y = (size.height - this.offsetUp) / 2;
        area.size = new cc.Size(size.width - 10, this.offsetUp);
        //设置路基碰撞体
        if (isBoxCollider) {
            let area = this.node.addComponent(cc.BoxCollider);
            area.tag = TagWayBody;
            area.offset.y = collider.offset.y;
            area.size = new cc.Size(nodeSize.width + 4, nodeSize.height + 4);
        }
    }
}
